/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AltaArticulos.java
 *
 * Created on 19/07/2011, 01:58:02
 */

package ventanas;

import conexiones.AdministradorDeConexiones;
import dao.ArticulosDao;
import dao.RubrosDao;
import java.sql.Connection;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import modelo.Articulo;
import modelo.Rubro;

/**
 *
 * @author Cristian Perez
 */
public class AltaArticulos extends javax.swing.JDialog {
    private DefaultComboBoxModel modeloCombo = new DefaultComboBoxModel();

    /** Creates new form AltaArticulos */
    public AltaArticulos(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();

        jComboRubros.setModel(modeloCombo);
        modeloCombo = (DefaultComboBoxModel) jComboRubros.getModel();
        CargarComboRubros();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblCodigo = new javax.swing.JLabel();
        lblDescripcion = new javax.swing.JLabel();
        lblPrecioCosto = new javax.swing.JLabel();
        lblPrecioVenta = new javax.swing.JLabel();
        lblStkMin = new javax.swing.JLabel();
        lblStkActual = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        txtDescripcion = new javax.swing.JTextField();
        txtPrecioCosto = new javax.swing.JTextField();
        txtPrecioVenta = new javax.swing.JTextField();
        txtStkMin = new javax.swing.JTextField();
        txtStkAct = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jComboRubros = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        lblPorcVenta = new javax.swing.JLabel();
        txtPorcVenta = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        btnGuardar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Alta Articulos");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        lblCodigo.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblCodigo.setText("Codigo: ");

        lblDescripcion.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblDescripcion.setText("Descripcion: ");

        lblPrecioCosto.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblPrecioCosto.setText("Precio Costo: ");

        lblPrecioVenta.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblPrecioVenta.setText("Precio Venta: ");

        lblStkMin.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblStkMin.setText("Stock Minimo: ");

        lblStkActual.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblStkActual.setText("Stock Actual: ");

        txtCodigo.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        txtCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCodigoKeyPressed(evt);
            }
        });

        txtDescripcion.setFont(new java.awt.Font("Tahoma", 3, 11));
        txtDescripcion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDescripcionKeyPressed(evt);
            }
        });

        txtPrecioCosto.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        txtPrecioCosto.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPrecioCostoFocusLost(evt);
            }
        });
        txtPrecioCosto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPrecioCostoKeyPressed(evt);
            }
        });

        txtPrecioVenta.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        txtPrecioVenta.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtPrecioVentaFocusGained(evt);
            }
        });
        txtPrecioVenta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPrecioVentaKeyPressed(evt);
            }
        });

        txtStkMin.setFont(new java.awt.Font("Tahoma", 3, 11));
        txtStkMin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtStkMinKeyPressed(evt);
            }
        });

        txtStkAct.setFont(new java.awt.Font("Tahoma", 3, 11));
        txtStkAct.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtStkActKeyPressed(evt);
            }
        });

        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("*");

        jLabel2.setForeground(new java.awt.Color(255, 0, 0));
        jLabel2.setText("*");

        jLabel3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel3.setText("*");

        jLabel4.setForeground(new java.awt.Color(255, 0, 0));
        jLabel4.setText("*");

        jLabel5.setForeground(new java.awt.Color(255, 0, 0));
        jLabel5.setText("(*) Campos obligatorios");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel6.setText("Rubro: ");

        jComboRubros.setFont(new java.awt.Font("Tahoma", 3, 11));
        jComboRubros.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jComboRubrosKeyPressed(evt);
            }
        });

        jLabel7.setForeground(new java.awt.Color(255, 0, 51));
        jLabel7.setText("*");

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jButton1.setText("+");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        lblPorcVenta.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblPorcVenta.setText("% de Venta: ");

        txtPorcVenta.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        txtPorcVenta.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtPorcVentaFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPorcVentaFocusLost(evt);
            }
        });
        txtPorcVenta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPorcVentaKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblDescripcion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblCodigo)
                            .addComponent(lblPrecioCosto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(31, 31, 31)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtPrecioCosto, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(61, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPrecioVenta)
                            .addComponent(lblPorcVenta)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(127, 127, 127)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtPorcVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtPrecioVenta, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addGap(265, 265, 265))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblStkActual)
                            .addComponent(lblStkMin))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jComboRubros, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton1))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtStkAct, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtStkMin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(134, Short.MAX_VALUE))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(157, 157, 157)
                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodigo)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDescripcion)
                    .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPrecioCosto)
                    .addComponent(txtPrecioCosto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPorcVenta)
                    .addComponent(txtPorcVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPrecioVenta)
                    .addComponent(txtPrecioVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtStkMin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblStkMin))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtStkAct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblStkActual))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboRubros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jButton1)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addContainerGap(34, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnGuardar.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnSalir.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(111, 111, 111)
                .addComponent(btnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                .addGap(55, 55, 55))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10, 10, 10))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-548)/2, (screenSize.height-496)/2, 548, 496);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        String codBarra = txtCodigo.getText().trim();
        if (validarFormulario(codBarra)) {
            Connection conn = null;
            try {
                conn = AdministradorDeConexiones.obtenerConexion();
                long codigo;
                //Verificamos los primero digitos del codigo de barra para identificar de que fuente viene el producto
                //(Si es con codigo predeterminado o generado por la balanza)Syste,
                
                //Obtenemos el primer digito del codigo para obtener la fuente
                if (codBarra.charAt(0) == '2') {
                    String codAcargar = codBarra.substring(0, 6);
                    codigo = Long.parseLong(codAcargar);
                }
                else codigo = Long.parseLong(txtCodigo.getText());

                String desc = txtDescripcion.getText();
                float preCosto = Float.parseFloat(txtPrecioCosto.getText().trim());
                float preVenta = Float.parseFloat(txtPrecioVenta.getText().trim());
                float porcentaje = Float.parseFloat(txtPorcVenta.getText().trim());
                
                //Si el campo stock minimo no esta vacio lo parseo a integer
                int stkMin = 0;
                if (!txtStkMin.getText().equals("")) {
                    stkMin = Integer.parseInt(txtStkMin.getText().trim());
                }
                
                //Si el campo stock actual no esta vacio lo parseo a integer
                int stkAct = 0;
                if (!txtStkAct.getText().equals("")) {
                    stkAct = Integer.parseInt(txtStkAct.getText().trim());
                }
                
                Rubro rub =(Rubro)jComboRubros.getSelectedItem();
                Articulo art = new Articulo(codigo, desc, preCosto, porcentaje, preVenta, stkAct, stkMin, rub);
                ArticulosDao.getInstance().insertar(conn, art);
                JOptionPane.showMessageDialog(this, "El articulo ha sido guardado!!!", "Guardar", JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
            }catch(Exception e) {e.printStackTrace();}
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void txtCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            if (!txtCodigo.getText().equals("")) {
                txtDescripcion.requestFocus();
            }
        }
    }//GEN-LAST:event_txtCodigoKeyPressed

    private void txtDescripcionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDescripcionKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            if (!txtDescripcion.getText().equals("")) {
                txtPrecioCosto.requestFocus();
            }
        }
    }//GEN-LAST:event_txtDescripcionKeyPressed

    private void txtPrecioVentaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPrecioVentaKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            if (!txtPrecioVenta.getText().equals("")) {
                txtStkMin.requestFocus();
            }
        }
    }//GEN-LAST:event_txtPrecioVentaKeyPressed

    private void txtStkMinKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtStkMinKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
                txtStkAct.requestFocus();
        }
    }//GEN-LAST:event_txtStkMinKeyPressed

    private void txtStkActKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtStkActKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
           jComboRubros.requestFocus(); 
        }
    }//GEN-LAST:event_txtStkActKeyPressed

    private void jComboRubrosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jComboRubrosKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            btnGuardar.requestFocus();
        }
    }//GEN-LAST:event_jComboRubrosKeyPressed

    private void txtPrecioCostoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPrecioCostoKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            if (!txtPrecioCosto.getText().equals("")) {
                txtPorcVenta.requestFocus();
            }
        }
    }//GEN-LAST:event_txtPrecioCostoKeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        ABMRubros ventaABMRubros = new ABMRubros(this, true);
        ventaABMRubros.setVisible(true);
        modeloCombo.removeAllElements();
        CargarComboRubros();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtPorcVentaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPorcVentaKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
          if (validarPrecios()) {
            try {
                float porciento = Float.parseFloat(txtPorcVenta.getText().trim());
                float preCosto = Float.parseFloat(txtPrecioCosto.getText().trim());
                //Paso el paseo, es un numero
                txtPrecioVenta.setText(String.valueOf(preCosto + (preCosto * porciento / 100)));
                txtStkMin.requestFocus();
            }catch(Exception e){
                e.printStackTrace();
            }
          }
        }
    }//GEN-LAST:event_txtPorcVentaKeyPressed

    private void txtPorcVentaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPorcVentaFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPorcVentaFocusLost

    private void txtPrecioCostoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPrecioCostoFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPrecioCostoFocusLost

    private void txtPorcVentaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPorcVentaFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPorcVentaFocusGained

    private void txtPrecioVentaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPrecioVentaFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPrecioVentaFocusGained

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboRubros;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblDescripcion;
    private javax.swing.JLabel lblPorcVenta;
    private javax.swing.JLabel lblPrecioCosto;
    private javax.swing.JLabel lblPrecioVenta;
    private javax.swing.JLabel lblStkActual;
    private javax.swing.JLabel lblStkMin;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtPorcVenta;
    private javax.swing.JTextField txtPrecioCosto;
    private javax.swing.JTextField txtPrecioVenta;
    private javax.swing.JTextField txtStkAct;
    private javax.swing.JTextField txtStkMin;
    // End of variables declaration//GEN-END:variables

    private void CargarComboRubros() {
        Connection conn = null;
        ArrayList lista = null;
        try {
            conn = AdministradorDeConexiones.obtenerConexion();
            lista = RubrosDao.getInstance().getTodos(conn);
            Rubro rub = null;
            for (int i=0; i < lista.size(); i++) {
                rub = (Rubro)lista.get(i);
                modeloCombo.addElement(rub);
            }
        }catch(Exception e) {e.printStackTrace();}
    }

   private boolean validarFormulario(String codB) {
        long valor;
        //Validamos que el campo codigo no este vacio
        if (codB.trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un codigo...", "Error", JOptionPane.WARNING_MESSAGE);
            txtCodigo.requestFocus();
            return false;
        }

        //Se lanza una excepcion si el codigo NO es un entero
        try {
            if (codB.charAt(0) == '2') {
                    String codAcargar = codB.substring(0, 6);
                    valor = Long.parseLong(codAcargar);
                }
            else valor = Long.parseLong(codB);
            if (valor < 1) {
                JOptionPane.showMessageDialog(this, "Debe ingresar un numero mayor que Cero...", "Error", JOptionPane.WARNING_MESSAGE);
                txtCodigo.requestFocus();
                return false;
            }
        }catch(Exception NumberFormatException) {
            JOptionPane.showMessageDialog(this, "El codigo debe ser un numero entero!", "Error", JOptionPane.WARNING_MESSAGE);
            txtCodigo.requestFocus();
            txtCodigo.setText("");
            return false;
        }

        //Validamos que el codigo no exista en la base
        Connection conn = null;
        try {
            conn = AdministradorDeConexiones.obtenerConexion();
            if (ArticulosDao.getInstance().verificarExistenciaArt(conn, valor)) {
                JOptionPane.showMessageDialog(this, "El codigo ya existe...", "Error", JOptionPane.WARNING_MESSAGE);
                txtCodigo.requestFocus();
                txtCodigo.setText("");
                return false;
            }
        }catch(Exception e) {e.printStackTrace();}
      


        //Validamos que el campo descripcion no este vacio
        if (txtDescripcion.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar una descripcion...", "Error", JOptionPane.WARNING_MESSAGE);
            txtDescripcion.requestFocus();
            return false;
        }

        //Validamos que el campo precio costo no este vacio
        if (txtPrecioCosto.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un precio de costo...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPrecioCosto.requestFocus();
            txtPrecioCosto.setText("");
            return false;
        }

        //Se lanza una excepcion en caso de que el precio costo no sea un numero real
        try {
            Double.parseDouble(txtPrecioCosto.getText().trim());
        }catch(Exception NumberFormatException) {
            JOptionPane.showMessageDialog(this, "El precio de costo debe ser un numero...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPrecioCosto.requestFocus();
            txtPrecioCosto.setText("");
            return false;
        }

        //Validamos que el campo precio venta no este vacio
        if (txtPrecioVenta.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un precio de venta...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPrecioVenta.requestFocus();
            return false;
        }

        //Se lanza una excepcion en caso de que el precio venta no sea un numero real
        try {
            Double.parseDouble(txtPrecioVenta.getText().trim());
        }catch(Exception NumberFormatException) {
            JOptionPane.showMessageDialog(this, "El precio de venta debe ser un numero real!", "Error", JOptionPane.WARNING_MESSAGE);
            txtPrecioVenta.requestFocus();
            txtPrecioVenta.setText("");
            return false;
        }

        //Validamos que el valor del campo stock minimo sea correcto
        if (txtStkMin.getText().equals("")) {
            txtStkMin.setText("0");
        }
           try {
                Integer.parseInt(txtStkMin.getText().trim());
           }catch(Exception NumberFormatException) {
                JOptionPane.showMessageDialog(this, "El stock minimo debe ser un numero entero!", "Error", JOptionPane.WARNING_MESSAGE);
                txtStkMin.requestFocus();
                txtStkMin.setText("");
                return false;
           }
        

         //Validamos que el valor del campo stock actual sea correcto
        if (txtStkAct.getText().trim().equals("")) {
            txtStkAct.setText("0");
        }
            try {
                Integer.parseInt(txtStkAct.getText().trim());
           }catch(Exception NumberFormatException) {
                JOptionPane.showMessageDialog(this, "El stock actual debe ser un numero entero!", "Error", JOptionPane.WARNING_MESSAGE);
                txtStkAct.requestFocus();
                txtStkAct.setText("");
                return false;
           }
        

        //Validamos que existan rubros cargadas en la base de datos
        if (jComboRubros.getItemCount() == 0) {
            JOptionPane.showMessageDialog(this, "No existen rubros, debe cargarlos antes de ingresar articulos", "Error", JOptionPane.WARNING_MESSAGE);
            return false;
        }

       return true;
    }

   private boolean validarPrecios() {
        //Validamos que el campo precio costo no este vacio
        if (txtPrecioCosto.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un precio de costo...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPrecioCosto.requestFocus();
            return false;
        }

        //Se lanza una excepcion en caso de que el precio costo no sea un numero real
        try {
            Double.parseDouble(txtPrecioCosto.getText().trim());
        }catch(Exception NumberFormatException) {
            JOptionPane.showMessageDialog(this, "El precio de costo debe ser un numero...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPrecioCosto.requestFocus();
            txtPrecioCosto.setText("");
            return false;
        }

        //Validamos que el campo porcentaje de venta no este vacio
        if (txtPorcVenta.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un porcentaje de venta...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPorcVenta.setText("");
            txtPorcVenta.requestFocus();
            return false;
        }

        //Se lanza una excepcion en caso de que el porcentaje de venta no sea un numero
        try {
            Double.parseDouble(txtPorcVenta.getText().trim());
        }catch(Exception NumberFormatException) {
            JOptionPane.showMessageDialog(this, "El porcentaje de venta debe ser un numero...", "Error", JOptionPane.WARNING_MESSAGE);
            txtPrecioCosto.requestFocus();
            txtPrecioCosto.setText("");
            return false;
        }
 
       return true;
   }
}
